<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class User_info extends CI_Controller{
	public function __construct($uid=1) {
		parent::__construct ();
		$this->tologin->isLogin ( site_url ( 'login' ) );
		$this->load->model("user_model");
		$this->load->model("project_model");
		$this->load->model("category_model");
		$this->load->model("project_participater_model");
		$this->load->model('category_model');
		$whereArr = array("status"=>1);
		$this->category_list = $this->category_model->resultAll($whereArr);
	}
	/*
	 *个人信息
	 */
	public function index($uid=1){
		$user_message = $this->user_message($uid);
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$whereArr = array("uid"=>$uid,"status"=>1);
		$project_list = $this->project_model->resultAll($whereArr,"starttime@DESC");
		foreach ($project_list as $key => $value){
			$category = $this->change_cid_to_cname($value['cid']);
			$project_list[$key]['cname'] = $category['cname'];
			$project_list[$key]['join_count'] = $this->join_project_num($value['pid']);
			$project_list[$key]['isEnd'] = $this->count_day($value['starttime'], $value['addtime']);
			$project_list[$key]['logo'] = ltrim($value['logo'],".");
		}
		$data['user_message'] = $user_message;
		$data['project_list'] = $project_list;
		$data['category_list'] = $this->category_list;
		$this->load->view("user_info",$data);
	}
	/*
	 *用户信息
	 */
	public function user_message($uid){
		$whereArr = array('uid'=>$uid);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user;
		}
	}
	/*
	 *根据分类id查找分类名数据
	 */
	public function change_cid_to_cname($cid){
		$whereArr = array("cid"=>$cid,"status"=>1);
		$category = $this->category_model->row($whereArr);
		if($category){
			return $category;
		}
	}
	/*
	 *查找项目参加的人数
	 */
	public function join_project_num($pid){
		$whereArr=array('pid'=>$pid,"status"=>1);
		$count = $this->project_participater_model->count($whereArr);
		return $count;
	}
	/*
	 *计算项目剩余天数
	 */
	public function count_day($starttime,$addtime){
		$nowtime = time();
		$time_difference_day = round(($nowtime - $starttime)/3600/24);
		if($time_difference_day<$addtime){
			return ($addtime-$time_difference_day);
		}else{
			return false;
		}
	}
}